package com.nd.mobile.study.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.nd.mobile.study.R;
import com.nd.mobile.study.common.BaseActivity;
import com.nd.mobile.study.common.SimpleAsyncTask;
import com.nd.mobile.study.common.util.StringUtils;
import com.nd.mobile.study.domain.User;
import com.nd.mobile.study.view.helper.ToastHelper;
import com.nd.mobile.study.view.helper.Validator;
import com.nd.mobile.study.view.widget.Loading;

/**
 * 注册界面
 * 
 */
public class RegisterActivity extends BaseActivity {
	
	static String TAG = "RegisterActivity";
	
    private EditText mEdtUsername = null;

    private EditText mEdtPassword = null;
    
    private EditText mEdtMobile = null;
    
    private EditText mEdtEmail = null;
    
    private Button mBtnRegister = null;

    @Override
    protected void onBaseCreate(Bundle savedInstanceState) {
        setContentView(R.layout.register);
    }

    @Override
    protected void onAfterCreate(Bundle savedInstanceState) {
    }

    /**
     * 控件初始化，事件初始化
     */
    @Override
    protected void initViews() {
    	this.initHeader();
    	//控件初始化
        this.mEdtUsername = (EditText) findViewById(R.id.register_edt_username);
        this.mEdtPassword = (EditText) findViewById(R.id.register_edt_password);
        this.mEdtMobile = (EditText) findViewById(R.id.register_edt_mobile);
        this.mEdtEmail = (EditText) findViewById(R.id.register_edt_email);
        this.mBtnRegister = (Button) findViewById(R.id.register_btn_register);
        
        // 注册按钮事件
        this.mBtnRegister.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                register();
            }
        });
    }

    @Override
    protected void initHeader() {
    	super.initHeader();
    	this.mHeaderBtnLogin.setVisibility(View.GONE);
    	this.mHeaderTitleTv.setText(R.string.btn_txt_register);
    }
    
    /**
     * 登陆事件
     */
    public void register() {
        // 获取用户名和密码
        String username = this.mEdtUsername.getText().toString().trim();
        String password = this.mEdtPassword.getText().toString().trim();
        String mobile = this.mEdtMobile.getText().toString().trim();
        String email = this.mEdtEmail.getText().toString().trim();

        // 检查用户名和密码
        if(!isValid()) return;

        this.mLoading.p();
        //执行登录
        RegisterTask task = new RegisterTask(this);
        task.execute(new String[]{username,password,mobile,email});
    }
    
	class RegisterTask extends SimpleAsyncTask<String, Integer, User> {

		private String username;
		private String password;
		
		public RegisterTask(Context context) {
			super(context);
		}

		@Override
		protected User onLoad(String... params) throws Exception {
			this.username = params[0];
			this.password = params[1];
			String mobile = params[2];
			String email = params[3];
			//格式化输出
			if(StringUtils.isBlank(mobile)){
				mobile = "";
			}
			if(StringUtils.isBlank(email)){
				email = "";
			}
			User.register(this.username, this.password,mobile,email);
			return null;
		}

		@Override
		protected void onUI(User result) {
	        // 保存登陆数据至shared文件中
	        User.saveUserInfoIntoSharedPreferences(RegisterActivity.this.mApp, this.username, this.password);
			ToastHelper.displayToast(RegisterActivity.this, "注册成功");
	        Intent intent = new Intent();
			intent.setClass(RegisterActivity.this, InitActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			finish();
			startActivity(intent);
		}
	}
	
    private boolean isValid() {
    	Validator validator = new Validator(this);
		return validator.validateUserName(this.mEdtUsername)
			&& validator.validatePassword(this.mEdtPassword);
	}
    
}
